package com.example.boosttestandroid;

import android.app.Application;
import android.content.Context;
import android.content.Intent;

import com.example.boosttestandroid.activity.AboutActivity;
import com.example.boosttestandroid.fragment.UserFragment;
import com.idlefish.flutterboost.FlutterBoost;
import com.idlefish.flutterboost.FlutterBoostDelegate;
import com.idlefish.flutterboost.FlutterBoostRouteOptions;
import com.idlefish.flutterboost.containers.FlutterBoostActivity;

import io.flutter.embedding.android.FlutterActivityLaunchConfigs;

public class App extends Application {

    public static Context app;

    @Override
    public void onCreate() {
        super.onCreate();
        app = getApplicationContext();
        FlutterBoost.instance().setup(this, new FlutterBoostDelegate() {
            @Override
            public void pushNativeRoute(FlutterBoostRouteOptions options) {
                // 这里根据options.pageName来判断你想跳转哪个页面，这里简单给一个
                if (options.pageName().equals("about")) {
                    Intent intent = new Intent(FlutterBoost.instance().currentActivity(), AboutActivity.class);
                    String data = (String) options.arguments().get("content");
                    intent.putExtra("content", data);
                    FlutterBoost.instance().currentActivity().startActivityForResult(intent, options.requestCode());
                }
            }

            @Override
            public void pushFlutterRoute(FlutterBoostRouteOptions options) {
                Intent intent = new FlutterBoostActivity.CachedEngineIntentBuilder(FlutterBoostActivity.class)
                        .backgroundMode(options.opaque() ? FlutterActivityLaunchConfigs.BackgroundMode.opaque : FlutterActivityLaunchConfigs.BackgroundMode.transparent)
                        .destroyEngineWithActivity(false)
                        .uniqueId(options.uniqueId())
                        .url(options.pageName())
                        .urlParams(options.arguments())
                        .build(FlutterBoost.instance().currentActivity());
                FlutterBoost.instance().currentActivity().startActivity(intent);
            }

            @Override
            public boolean popRoute(FlutterBoostRouteOptions options) {
                return FlutterBoostDelegate.super.popRoute(options);
            }
        }, engine -> {
        });
    }

    public static Context Instance() {
        return app;
    }
}
